"""
插件系统API路由
"""
from fastapi import APIRouter, HTTPException, Depends
from typing import List, Dict, Any
from pydantic import BaseModel

from .plugin_manager import PluginManager
from .base import PluginConfig

router = APIRouter()

# 全局插件管理器实例
plugin_manager = PluginManager()


class PluginResponse(BaseModel):
    """插件响应模型"""
    name: str
    status: Dict[str, Any]
    info: Dict[str, Any]
    config: Dict[str, Any]


class PluginConfigUpdate(BaseModel):
    """插件配置更新模型"""
    enabled: bool = False
    settings: Dict[str, Any] = {}
    auto_start: bool = False


@router.get("/", response_model=List[PluginResponse])
async def get_all_plugins():
    """获取所有插件"""
    return plugin_manager.get_all_plugins()


@router.get("/active", response_model=List[PluginResponse])
async def get_active_plugins():
    """获取活跃插件"""
    return plugin_manager.get_active_plugins()


@router.get("/{plugin_name}", response_model=PluginResponse)
async def get_plugin(plugin_name: str):
    """获取指定插件"""
    plugin = plugin_manager.get_plugin(plugin_name)
    if not plugin:
        raise HTTPException(status_code=404, detail=f"插件 {plugin_name} 不存在")
    return plugin


@router.post("/{plugin_name}/start")
async def start_plugin(plugin_name: str):
    """启动插件"""
    success = plugin_manager.start_plugin(plugin_name)
    if not success:
        raise HTTPException(status_code=400, detail=f"启动插件 {plugin_name} 失败")
    return {"message": f"插件 {plugin_name} 启动成功"}


@router.post("/{plugin_name}/stop")
async def stop_plugin(plugin_name: str):
    """停止插件"""
    success = plugin_manager.stop_plugin(plugin_name)
    if not success:
        raise HTTPException(status_code=400, detail=f"停止插件 {plugin_name} 失败")
    return {"message": f"插件 {plugin_name} 停止成功"}


@router.post("/{plugin_name}/reload")
async def reload_plugin(plugin_name: str):
    """重新加载插件"""
    success = plugin_manager.reload_plugin(plugin_name)
    if not success:
        raise HTTPException(status_code=400, detail=f"重新加载插件 {plugin_name} 失败")
    return {"message": f"插件 {plugin_name} 重新加载成功"}


@router.put("/{plugin_name}/config")
async def update_plugin_config(plugin_name: str, config: PluginConfigUpdate):
    """更新插件配置"""
    plugin_config = PluginConfig(
        enabled=config.enabled,
        settings=config.settings,
        auto_start=config.auto_start
    )
    
    success = plugin_manager.update_plugin_config(plugin_name, plugin_config)
    if not success:
        raise HTTPException(status_code=404, detail=f"插件 {plugin_name} 不存在")
    
    return {"message": f"插件 {plugin_name} 配置更新成功"}


@router.delete("/{plugin_name}")
async def unload_plugin(plugin_name: str):
    """卸载插件"""
    success = plugin_manager.unload_plugin(plugin_name)
    if not success:
        raise HTTPException(status_code=404, detail=f"插件 {plugin_name} 不存在")
    return {"message": f"插件 {plugin_name} 卸载成功"}


@router.get("/{plugin_name}/routes")
async def get_plugin_routes(plugin_name: str):
    """获取插件路由"""
    plugin = plugin_manager.get_plugin(plugin_name)
    if not plugin:
        raise HTTPException(status_code=404, detail=f"插件 {plugin_name} 不存在")
    
    # 获取插件的路由信息
    plugin_instance = plugin_manager.plugins.get(plugin_name)
    if plugin_instance:
        routes = plugin_instance.get_routes()
        return {"routes": routes}
    
    return {"routes": []} 